package everyday;

import java.util.Comparator;
import java.util.Map;
import java.util.TreeMap;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @ClassName D80
 * @Description https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array-ii/submissions/
 * @Author Yin
 * @Date 2021/4/6 9:48
 * @Version 1.0
 */
public class D80 {
    public int removeDuplicates(int[] nums) {
        Map<Integer, Integer> ans = new TreeMap<>(Comparator.comparingInt(o -> o));
        for (int num : nums) {
            if (ans.get(num) == null) {
                ans.put(num, 1);
            } else if (ans.get(num) == 1) {
                ans.put(num, 2);
            }
        }
        AtomicInteger index = new AtomicInteger();
        ans.forEach((k, v) -> {
            while (v-- > 0) {
                nums[index.get()] = k;
                index.getAndIncrement();
            }
        });
        return index.get();
    }
}
